package com.aj.activity;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.RadioGroup;

import com.aj.activity.adapter.HomeFragmentPagerAdapter;
import com.aj.activity.fragment.ChooseHomePhotoFragment;
import com.aj.activity.fragment.ChoosePersonPhotoFragment;

public class SharePhotoActivity extends BaseFragmentActivity {

	private RadioGroup photoview_tools;

	private ViewPager bodys;

	private ArrayList<Fragment> fragmentList;

	public static SharePhotoActivity instance;

	public static Boolean isLoadFlag = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_share_photo);
		initView();
	}

	private void initView() {
		instance = this;
		isLoadFlag = false;
		showProgressDialog();
		photoview_tools = (RadioGroup) findViewById(R.id.photoview_tools);
		bodys = (ViewPager) findViewById(R.id.bodys);
		bodys.setOffscreenPageLimit(0);
		ChoosePersonPhotoFragment choosePersonPhotoFragment = new ChoosePersonPhotoFragment();
		ChooseHomePhotoFragment chooseHomePhotoFragment = new ChooseHomePhotoFragment();
		fragmentList = new ArrayList<Fragment>();
		fragmentList.add(choosePersonPhotoFragment);
		fragmentList.add(chooseHomePhotoFragment);
		bodys.setAdapter(new HomeFragmentPagerAdapter(
				getSupportFragmentManager(), fragmentList));
		// ViewPager显示第一个Fragment
		bodys.setCurrentItem(0);
		// ViewPager页面切换监听
		bodys.setOnPageChangeListener(new myOnPageChangeListener());
		photoview_tools.setOnCheckedChangeListener(new CheckChangeListener());
	}

	public void dimiss() {
		dimissProgressDialog();
	}

	/**
	 * RadioButton切换Fragment
	 */
	private class CheckChangeListener implements
			RadioGroup.OnCheckedChangeListener {

		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			switch (checkedId) {
			case R.id.choose_person_photo:
				// ViewPager显示第一个Fragment且关闭页面切换动画效果
				bodys.setCurrentItem(0, false);
				break;
			case R.id.choose_home_photo:
				bodys.setCurrentItem(1, false);
				break;
			}
		}
	}

	/**
	 * ViewPager切换Fragment,RadioGroup做相应变化
	 */
	private class myOnPageChangeListener implements
			ViewPager.OnPageChangeListener {

		@Override
		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {

		}

		@Override
		public void onPageSelected(int position) {
			switch (position) {
			case 0:
				photoview_tools.check(R.id.choose_person_photo);
				break;
			case 1:
				photoview_tools.check(R.id.choose_home_photo);
				break;
			}
		}

		@Override
		public void onPageScrollStateChanged(int state) {

		}
	}
}
